import GameCtr from "../../Controller/GameCtr";
import GameDataInstance from "../../Common/GameDataInstance";
import vv from "../../Common/vv";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class loading extends cc.Component {

    onLoad() {

        this.scheduleOnce(() => {
           cc.director.loadScene('Start')
        }, 6);

    }

    start() {

        GameDataInstance.Instance.videoid = tt.createRewardedVideoAd({adUnitId:"24vsn5dvqhd2bsf33e"})

        tt.getSystemInfo(
            {
                 success:(res)=>
                 {
                     console.log('返回的屏幕宽高11',res)
                     GameDataInstance.Instance.windowheigh = res.screenHeight
                     GameDataInstance.Instance.windowwidth = res.screenWidth

                     console.log("屏幕宽度",GameDataInstance.Instance.windowwidth)
                     console.log("屏幕高度",GameDataInstance.Instance.windowheigh)
                     
        GameDataInstance.Instance.bannerAd =  tt.createBannerAd({
            adUnitId: '3h919hbkl5bd7mmmds',
            style: {
              left: 100,
              top: GameDataInstance.Instance.windowheigh-150,
              width: GameDataInstance.Instance.windowwidth,
            }
          })
                 },
                 fail:(res)=>
                 {
                    console.log('请求屏幕返回数据失败',res)
                 }
            })

        GameDataInstance.Instance.openid = cc.sys.localStorage.getItem('openid')

        console.log('查看openid',GameDataInstance.Instance.openid)

        if(GameDataInstance.Instance.openid.length<10)
        {
            console.log('第35次登陆')
            if(tt)
            {
                 tt.login({
                    success: function (res) {
                        GameDataInstance.Instance.userisLogin = true
                        console.log('code数据',res.code)

                        vv.sendPostHttpRequest(GameDataInstance.Instance.getopenidUrl,{'code':res.code},(res)=>
                        {
                            console.log('换取openid',res)
                            if(res.status == '1'||res.status == 1)
                            {
                                console.log('走了这里没?')
                              //  let data = JSON.parse(res.data)
                                GameDataInstance.Instance.openid = res.data.openid
                                cc.sys.localStorage.setItem('openid',GameDataInstance.Instance.openid)
                                console.log('请求的openid',GameDataInstance.Instance.openid)

                                //检查用户是否存在

                                vv.sendPostHttpRequest(GameDataInstance.Instance.hasopenid,{"openid": GameDataInstance.Instance.openid},(res)=>
                                {
                                    console.log('检查用户是否存在',res)
                                      if(res.msg != 'openid不存在')
                                      {
                                         console.log('用户已经注册')
                                       
                                     vv.sendPostHttpRequest(GameDataInstance.Instance.chaxunjifen,{'openid':GameDataInstance.Instance.openid},(res)=>
                                         {
                                               console.log('查询用户信息',res)
                                               if(res.status==1)
                                               {
                                                GameDataInstance.Instance.MaxScore = res.data.memberInfo.credit1
                                               }
                                          })
                                      }
                                      else
                                      {
                                          console.log('用户不存在重新注册')
                                          window['wx'].getUserInfo({
                                            success: (res) => {
                                                let userInfo = res.userInfo;
                                                let nickName = userInfo.nickName;
                                                let avatarUrl = userInfo.avatarUrl;
                                                let gender = userInfo.gender; //性别 0：未知、1：男、2：女
                                                let province = userInfo.province;
                                                let city = userInfo.city;
                                                let country = userInfo.country;
                                                let data = "openid=" + GameDataInstance.Instance.openid  + "&gender=" + gender + "&avatar=" + avatarUrl + "&nickname=" + nickName;
                                                vv.sendPostHttpRequest(GameDataInstance.Instance.updatememberUrl, data, (text) => {
                                                    if (text.status == "1"||text.status == 1) {
                                                        console.log('用户注册成功',text);

                                                        vv.sendPostHttpRequest(GameDataInstance.Instance.chaxunjifen,{'openid':GameDataInstance.Instance.openid},(res)=>
                                                        {
                                                             console.log('查询用户信息',res)
                                                             if(res.status==1)
                                                             {
                                                              GameDataInstance.Instance.MaxScore = res.data.memberInfo.credit1
                                                             }
                                                        })
                                                    }
                                                });
                                            }
                                        });
                                      }
                                })
                          
                            }
                        })
                    }
                  })
            }
        }

        else
        {
            console.log('openid存在 不需要再登陆')
            vv.sendPostHttpRequest(GameDataInstance.Instance.hasopenid,{"openid": GameDataInstance.Instance.openid},(res)=>
            {
                console.log('检查用户是否存在',res)
                  if(res.msg != 'openid不存在')
                  {
                     console.log('用户已经注册')
                   
                 vv.sendPostHttpRequest(GameDataInstance.Instance.chaxunjifen,{'openid':GameDataInstance.Instance.openid},(res)=>
                     {
                           console.log('查询用户信息',res)
                           if(res.status==1)
                           {
                            GameDataInstance.Instance.MaxScore = res.data.memberInfo.credit1
                           }
                      })
                  }
                  else
                  {
                      console.log('用户不存在重新注册')
                      window['wx'].getUserInfo({
                        success: (res) => {
                            let userInfo = res.userInfo;
                            let nickName = userInfo.nickName;
                            let avatarUrl = userInfo.avatarUrl;
                            let gender = userInfo.gender; //性别 0：未知、1：男、2：女
                            let province = userInfo.province;
                            let city = userInfo.city;
                            let country = userInfo.country;
                            let data = "openid=" + GameDataInstance.Instance.openid  + "&gender=" + gender + "&avatar=" + avatarUrl + "&nickname=" + nickName;
                            vv.sendPostHttpRequest(GameDataInstance.Instance.updatememberUrl, data, (text) => {
                                if (text.status == "1"||text.status == 1) {
                                    console.log('用户注册成功',text);

                                    vv.sendPostHttpRequest(GameDataInstance.Instance.chaxunjifen,{'openid':GameDataInstance.Instance.openid},(res)=>
                                    {
                                         console.log('查询用户信息',res)
                                         if(res.status==1)
                                         {
                                          GameDataInstance.Instance.MaxScore = res.data.memberInfo.credit1
                                         }
                                    })
                                }
                            });
                        }
                    });
                  }
            }),(error)=>
            {
                 console.log('检查用户是否存在报错',error)
            }
      
        }

        vv.sendPostHttpRequest('https://sjh5.88youxi.cn/app/index.php?i=55&c=entry&do=getonthdp&m=wxs_api',{'zdid':228},(response)=>
        {
            console.log('分享请求返回的数据',response)
            GameDataInstance.Instance.thumb = response.data[0].thumb
            GameDataInstance.Instance.titie = response.data[0].title
        })


       
    }

    // update (dt) {}
}
